package com.itheima.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.itheima.common.Paging;
import com.itheima.common.R;
import com.itheima.dto.user.*;
import com.itheima.service.UserService;
import com.itheima.vo.BriefUserVo;
import com.itheima.vo.organization.OrganizationVo;
import com.itheima.vo.user.AllUserVo;
import com.itheima.vo.user.DetailedUserVo;
import com.itheima.vo.user.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * (User)表控制层
 * 用户相关操作的控制类
 *
 * @author makejava
 * @since 2022-10-01 22:01:17
 */
@RestController
@RequestMapping("user")
@Api(tags = "用户操作api")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 使用账号密码登录
     *
     * @param dto
     * @return
     */
//    @LogAnnotation(value = "登录")
    @ApiOperation(value = "账号密码登录接口", notes = "{传参示例" +
            "\n" +
            "    \"id\": \"19251104252\",\n" +
            "    \"password\": \"19251104252\"\n" +
            "}")
    @PostMapping("/loginByAccount")
    public R<UserVo> loginByAccount(@Validated @RequestBody AccountLoginDto dto) throws Exception {
        return R.success(userService.loginByAccount(dto));
    }


    /**
     * 获取图片验证码
     *
     * @param response
     * @throws IOException
     */
    @ApiOperation(value = "获取图片验证码接口")
    @GetMapping("/getCaptchaImg")
    public R<String> getCaptchaImg(@RequestParam("phone") String phone, HttpServletResponse response) throws IOException {
        return R.success(userService.getCaptchaImg(phone, response));
    }


    /**
     * 手机验证码登录
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "手机验证码登录")
    @PostMapping("/loginByPhone")
    public R<UserVo> loginByPhone(@Validated @RequestBody PhoneLoginDto dto) throws Exception {
        return R.success(userService.loginByPhone(dto));
    }

    /**
     * 获取手机验证码的方法
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "获取手机验证码", notes = "" +
            "测试时取消对应的api，仅在返回信息中显示手机验证码" + "\n" +
            "传参示例    " +
            "\"phone\": \"13609636664\",\n" +
            "    \"imageCode\": \"3g8me\"")
    @PostMapping("/getPhoneCode")
    public R<String> getPhoneCode(@Validated @RequestBody PhoneLoginBaseParam dto) throws Exception {
        userService.getPhoneCode(dto);
        return R.success("success");
    }


    /**
     * 更改用户信息(密码，电话)
     *
     * @return
     */
    @ApiOperation(value = "更新用户信息Api", notes = "前端传参eg:" +
            "{\n" +
            "    \"password\": \"19251104258\",\n" +
            "    \"phone\": \"13609636674\"\n" +
            "}")
    @PutMapping("/updateUserMessage")
    public R<String> updateUserMessage(@Validated @RequestBody UpdateUserMessageDto dto) throws Exception {
        return R.success(userService.updateUserMessage(dto));
    }


    /**
     * 更新用户头像
     *
     * @param updateFile 头像文件
     * @return 结果
     */
    @ApiOperation(value = "更新用户头像Api", notes = "前端传参eg:" +
            "form-data" +
            "image")
    @PutMapping("/updateUserImage")
    public R<String> updateImage(@RequestParam("image") MultipartFile updateFile) throws Exception {
        userService.updateImage(updateFile);
        return R.success("更新成功");
    }

    /**
     * 查看个人资料
     *
     * @return
     */
    @ApiOperation(value = "查看个人资料")
    @GetMapping("/showUserMessage")
    public R<DetailedUserVo> showUserMessage() throws Exception {
        return R.success(userService.showUserMessage());
    }

    /**
     * 获取用户简略信息
     *
     * @return
     */
    @ApiOperation(value = "获取用户简略信息")
    @PostMapping("/getBriefUser")
    public R<List<BriefUserVo>> getBriefUser(@RequestBody List<String> userIds) {
        return R.success(userService.getBriefUser(userIds));
    }

    /**
     * 展示所有用户(按层级)（发信息时使用）
     *
     * @return
     */
    // TODO: 2023/2/16 没有用到
    @ApiOperation(value = "展示所有用户（按层级）（发信息时使用）")
    @GetMapping("/showAllUsers")
    public R<OrganizationVo> showAllUsers() {
        return R.success(userService.showAllUsers());
    }

    /**
     * excel导入新用户
     *
     * @param file
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "excel导入新用户")
    @PutMapping("/excelImport")
    public R<String> excelImport(@RequestParam("file") MultipartFile file) throws Exception {
        userService.excelImport(file);
        return R.success("新用户导入完成");
    }

    /**
     * 批量删除用户
     *
     * @param userIdList
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "批量删除用户")
    @DeleteMapping("/deleteUser")
    public R<String> deleteUser(@RequestParam("userIdList") List<String> userIdList) throws Exception {
        userService.deleteUser(userIdList);
        return R.success("删除成功");
    }

    /**
     * 展示所有用户
     *
     * @param dto 展示所有用户筛选Dto
     * @return 管理员界面-所有用户信息vo
     */
    @ApiOperation(value = "展示所有用户")
    @PostMapping("/showAllUser")
    public R<Paging<AllUserVo>> showAllUser(@RequestBody AllUserDto dto){
        return R.success(userService.showAllUser(dto));
    }


    /**
     * 重置密码
     * @param userId 用户id
     * @return 结果
     */
    @ApiOperation(value = "重置密码")
    @PutMapping("/resetPassword")
    public R<String> resetPassword(@RequestParam("userId") String userId) throws Exception {
        userService.resetPassword(userId);
        return R.success("success");
    }

    public void test(){

    }
}

